myApp.controller('account.index', ['$scope', '$rootScope', 'accountService', function ($scope, $rootScope, accountService) {
    $scope.accountTable = {};
    $scope.selectedAccount = {};

    $scope.newAccount = {};

    $scope.$watch('$viewContentLoaded', function (event, msg) {
        initialAccountTable();
    });

    // accountService.getList().then(function (res) {
    //     var response = res.data;
    //     // console.log(response);
    //     initialAccountTable(response.data);
    // })

    /*
     * 档案数据表
     * */
    function initialAccountTable() {
        if ($('#account-table').hasClass('dataTable')) {
            $scope.accountTable.clear();
            $scope.accountTable.rows.add(data).draw();
        } else {
            $scope.accountTable = $('#account-table').DataTable({
                "dom": '<"row"<"col-md-6"B><"col-md-1"><"col-md-5"f>><tip>',
                "buttons": [
                    {
                        text: 'Create',
                        className: 'btn  btn-sm btn-default',
                        action: function (e, dt, node, config) {
                            $scope.addAccount();
                        }
                    }
                ],
                "paging": true,
                "pagingType": "full_numbers",
                "processing": true,
                "serverSide": true,
                "ajax": {
                    "url": config.backend_url + '/web/user',
                    "type": "POST",
                    "dataType": 'json',
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("Authorization", "Bearer " + $rootScope.accessToken);
                        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
                    },
                },
                "columns": [
                    {"data": "nickname"},
                    {"data": "email"},
                    {"data": "emailValidated"},
                    {"data": "obdCount"},
                    {"data": "vehicleCount"},
                    {"data": "remark"}
                ],
                "lengthChange": false,
                "searching": true,
                "retrieve": true,
                "order": [[1, 'asc']],//排序
                "columnDefs": [
                    {
                        "targets": [6],//设置表格第7列的数据显示
                        "render": function (data, type, full, meta) {
                            return '<button type="button" class="btn btn-xs btn-primary exam-update" style="margin-right: 10px" ><i class="md md-search"></i></button>'
                        }
                    },
                    {
                        "targets": [2],//设置表格第5列的数据显示
                        "render": function (data, type, full, meta) {
                            return data ? '<span class="label label-success">Yes</span>' : '<span class="label label-danger">No</span>';
                        }
                    }
                ],
                "ordering": false,
                "info": false,
                "autoWidth": false,
                "iDisplayLength": 9,
                "oLanguage": {//界面语言...
                    "oPaginate": {
                        "sPrevious": "&laquo;",
                        "sNext": "&raquo;"
                    },
                    "sSearch": "<i class='fa fa-search'></i>",
                    "sSearchPlaceholder": "Search Email",
                    "sZeroRecords": "没有检索到符合条件的数据",
                    'emptyTable': '无相应数据',
                    'loadingRecords': '加载中...',
                    'processing': '查询中...'
                }
            })
        }
    }

    /*
         * 获取表格行的数据
         * */
    $('#account-table').on('click','.btn',function(){
        $scope.selectRowData = $scope.accountTable.row($(this).parents('tr')).data();
        $('#user-profile-modal').modal('show');
        accountService.getDetail($scope.selectRowData.id).then(function (res) {
                var response = res.data;
                $scope.selectedAccount = response.data;
        });
        $(this).addClass('checked');

    });

    $scope.addAccount = function () {
        $('#accountModel').modal();
    }

    $scope.saveAccount = function () {
        var data={
            email:$scope.newAccount.email,
            password:$scope.newAccount.password
        };
        console.log(data);

        accountService.create(data).then(function(res){
            if(res.data.success == true){
                swal("Success!", res.data.msg, "success");
            }else{
                swal("Error, "+res.data.msg, res.data.msg, "error");
            }
        })
    }
}]);
